#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../include/hash.h"
//#include "hash.h"



Node *create(void *data)    
{
        Node *node;
        if(!(node=malloc(sizeof(Node)))) return NULL;
        node->data=data;
        node->next=NULL;
        return node;
}


Node *insertnode(Node *start, struct file_descriptor *fdes ) {
	
	
	Node *temp,*ptr;
	
	ptr = create(fdes);
	
	if(start == NULL)
		start = ptr;
	else {
		temp = start;
		while( temp -> next != NULL )
		{
			temp = temp -> next;
      }
		temp -> next = ptr;
	}
	return start;	
}

Node *fileSearch(char *nam) {
	int index,len;	
	Node *temp,*first,*head,*start;
	struct file_descriptor *f;
	head = malloc(sizeof(Node));	
	temp = head;
	index = hash_val(nam);
	if(array[index] == NULL)
		return NULL;
	start = array[index];
	len = strlen(nam);
       //printf("File %s in the path %s \n",((struct file_descriptor *)(start->data))->filename,((struct file_descriptor *)(start->data))->path);
	while (start != NULL) {
	   if(strncmp(((struct file_descriptor *)(start -> data)) -> filename,nam,len) == 0 )
            {     

		   f = malloc(sizeof(struct file_descriptor));		   
		   	
	           strcpy(f->path,((struct file_descriptor *)(start->data))->path);
	           strcpy(f->filename,((struct file_descriptor *)(start->data))->filename);				
		   //printf("File %s in the path %s \n",f->filename,f->path);	
		   first = create(f);		   
	           //strcpy(((struct file_descriptor *)(first->data))->filename,((struct file_descriptor *)(start->data))->filename);		
                   //printf("Loc: %s\n",((struct file_descriptor *)(first->data))->path);
                   temp -> next = first;
		   first -> next = NULL;
		   temp = first;	
            } 
            start = start -> next;

	}
	return head->next;
}
// display hash function

void display_hash() {
	int i;
	Node *temp;
	for( i =0; i< 23; i++) 
	{
		if ( array[i] == NULL ){
			//printf("\nposition %d is empty",i);
		}	
		else {
			temp = array[i];
			while( temp != NULL ) 
			{
				//printf("\nfiles at position %d\t",i);
				//printf("%s",(( struct file_descriptor*)(temp -> data)) -> filename);
				
					temp = temp -> next;
			}
		}		
	}
	
}





int hash_val(char *name)
{

	int hashkey = -1;
        int asciival = tolower(name[0]);
	//printf("The ascii value is %d",asciival);
        if(asciival >=97 && asciival <=122)
                hashkey = asciival - 97 + 1;
        else if(asciival == 32){
                //printf("space encountered in the name of file");
        }else                                                    	
                hashkey = -1;
    	
       
    if(hashkey == -1)
         return hashkey;
    else  
    {         
        hashkey = hashkey % hash_size;
         return hashkey;
    }
 
}





Node* delete_file1(Node *start, fd *fdes)		
{	Node *temp, *prev= start;
	
	temp = start;
	
	while(temp!=NULL && ((fd *)(temp->data)) != fdes) 
	{	prev = temp;
		temp=temp->next; 	
	}
	if(temp != NULL) {
		if(temp == start) 
			start = temp->next;
		else
			prev->next = temp->next;
		}
	
	return start;	
}

int searchfile1(char *name, char *outputfile)
{
        FILE *fp;
        int count=0;
	Node *temp,*start;
	temp =  fileSearch(name);
	start=temp;
	
	if(temp==NULL)
	  return 0;
	
	else
	{
          fp=fopen("outputfile","w");
           if(fp==NULL)
            	return 0;
            
            else
	     { 
               
               while(start!=NULL)
               {
                 count++;
		 fprintf(fp,"File name is %s\t",((struct file_descriptor *)(start->data))->filename);		
                 fprintf(fp,"File path is %s\n",((struct file_descriptor *)(start->data))->path);
		
                 start=start->next;
               }
                
             
             return count;
		}
	}
}


/*int main() {
	//char name1[100];
    //struct hashlist *h;
    
    char path[100];
	int index = 0;
    char filenam[100];
     char out[100];	
     int i;
     int ch;
     char name[100];
     struct file_descriptor *fdes;
	struct file_descriptor *fde;
     Node *tp;	
     do{
     printf ("enter the option \n 1.insert \n 2.display\n 3.search\n 4.searchprefix\n 5.delete\n0. to exit\n");
     scanf("%d",&i);
     switch(i)
     {
              case 1:
                       printf("Enter the filename\n"); 
                       scanf("%s",name);
                       printf("\n %s",name); 	
                      // index = hash_val(name[0]);
                         index = hash_val(name);
  	                   printf("\nthe hash value is %d\n",index);
  	                   printf("enter the file path\n");
  	                   scanf("%s",path);
  	                   printf("\n %s",path);
			   fdes = malloc(sizeof(struct file_descriptor));	   
			   strcpy(fdes->filename,name);
			
			   strcpy(fdes->path,path);
		
                           array[index]=insertnode(array[index],fdes);
                       break;   
               case 2:
                       display_hash(); 
                       break;     
               case 3:
                      printf("enter the name of the file\n");
                      scanf("%s",filenam);
                   //   search(filenam);
                      break;
		case 4:
			        
                      		printf("enter the name of the file\n");
                      		scanf("%s",filenam);
                                //Node *tem;
				//tem = malloc(sizeof(tem));
				
                                //tem=fileSearch(filenam);
				printf("name of output file");				
				scanf("\n%s",out);
				searchfile1(filenam,out);
                                /*if(tem == NULL)
                                	printf("No files found");
                                 else{
                                        printf("The files starting with %s are found in the paths \n",filenam);
                                  
				          while(tem !=NULL)
                                          {
                                                printf("File %s in the path %s \n",((struct file_descriptor *)(tem->data))->filename,((struct file_descriptor *)(tem->data))->path);
                                                tem=tem->next;
                                          }
                   
                                } 
                               break; 
		case 5:
				printf("enter the file to be deleted");
				scanf("%s",filenam);
				//printf("enter the file path\n");
  	                   	//scanf("%s",path);
  	                   	//fde=malloc(sizeof(struct file_descriptor));
								
				index = hash_val(filenam);
				//strcpy(fde->filename,filenam);
				//strcpy(fde->path,path);
				delete_file1(array[index],fdes);
				break;							
               default:
   //                    printf("enter the option");
                       break;
     }
     printf ("enter the option \n 1.continue \n 0. to exit\n");
     scanf("%d",&ch);
     }while(ch != 0); 
     
     
	// printf("%s",array[index]);
 
}*/








